<template>
  <el-dialog
    title="培训总结表"
    :visible.sync="sumUpData.dialogVisible"
    class="normal"
  >
    <!-- :before-close="handleClose" -->
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="temp"
      label-position="right"
      label-width="90px"
    >
      <!-- :disabled="disabled" -->
      <el-form-item label="总结时间">
        <div class="block">
          <el-date-picker
            v-model="temp.sumTime"
            type="date"
            :disabled="disabled"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </div>
      </el-form-item>
      <el-form-item label="培训时间">
        <div class="block">
          <el-date-picker
            v-model="temp.trainTime"
            :disabled="disabled"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00']"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </div>
      </el-form-item>
      <el-form-item label="培训内容">
        <el-input
          v-model="temp.content"
          :disabled="disabled"
          style="width: 500px"
          type="textarea"
          placeholder="请输入内容"
          max-length="200"
          show-word-limit
        />
      </el-form-item>
      <el-row>
        <el-col>
          <el-form-item label="有无证书">
            <el-radio-group v-model="temp.certificateFlag" :disabled="disabled">
              <el-radio :label="true">有</el-radio>
              <el-radio :label="false">无</el-radio>
            </el-radio-group>
          </el-form-item></el-col>
      </el-row>
      <el-form-item v-if="temp.certificateFlag" :span="8" label="证书编号">
        <el-input
          v-model="temp.certificateName"
          :disabled="temp.phaseNum >= 1"
          style="width: 500px"
          type="textarea"
          placeholder="请输入内容"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="总结内容">
        <el-input
          v-model="temp.sumContent"
          :disabled="temp.phaseNum > 0"
          style="width: 500px"
          type="textarea"
          placeholder="请输入内容"
          max-length="200"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        v-if="temp.phaseNum >= 2 || !temp.phaseNum"
        label="主管领导"
        prop="name"
      >
        <el-tag
          v-for="(tag, index) in temp.handleUserList"
          :key="index"
          :disable-transitions="false"
          style="margin-right: 10px"
          @close="StaffList(tag)"
        >
          {{ tag.label }}
        </el-tag>
        <el-button
          v-if="!temp.phaseNum"
          size="mini"
          round
          circle
          icon="el-icon-plus"
          @click="openReview(3, 1)"
        />
      </el-form-item>
      <el-form-item v-if="temp.phaseNum >= 1" label="意见1">
        <el-input
          v-model="temp.opinion1"
          :disabled="temp.phaseNum > 1"
          style="width: 500px"
          type="textarea"
          placeholder="请输入内容"
          max-length="200"
          show-word-limit
        />
      </el-form-item>
      <el-form-item v-if="temp.phaseNum == 1" label="人事行政" prop="name">
        <el-tag
          v-for="(tag, index) in temp.handleUserList2"
          :key="index"
          :disable-transitions="false"
          style="margin-right: 10px"
          @close="StaffList(tag)"
        >
          {{ tag.label }}
        </el-tag>
        <el-button
          v-if="temp.phaseNum == 1"
          size="mini"
          round
          circle
          icon="el-icon-plus"
          @click="openReview(3, 2)"
        />
      </el-form-item>
      <el-form-item v-if="temp.phaseNum > 1" label="意见2">
        <el-input
          v-model="temp.opinion2"
          style="width: 500px"
          type="textarea"
          placeholder="请输入内容"
          max-length="200"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="temp.comment"
          style="width: 500px"
          type="textarea"
          placeholder="请输入内容"
          max-length="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="sumUpData.dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
    <SearchPersonnel
      :search-personnel-data="SearchPersonnelData"
      @dataListData="dataListData"
    />
  </el-dialog>
</template>

<script>
import SearchPersonnel from '@/components/SearchUser/SearchPersonnel.vue'
import { save, opinion, listNode, adminAudit } from '@/api/ts/train.js'
export default {
  components: { SearchPersonnel },
  props: ['sumUpData'],
  data() {
    return {
      StaffNameList: [],
      temp: { handleUserList: [] },
      rules: {},
      SearchPersonnelData: {},
      personnelType: {},
      startTime: '',
      endTime: '',
      disabled: true
    }
  },

  watch: {
    sumUpData(data) {
      if (data.row.phaseNum == 1) {
        this.temp = data.row
        this.temp.trainTime = data.row.trainTime.split('/')
      } else if (data.row.phaseNum == 2) {
        this.temp = data.row
        this.temp.trainTime = data.row.trainTime.split('/')
      } else if (!data.row.phaseNum) {
        this.temp = data.row
        const Time = [data.row.startTime, data.row.endTime]
        this.startTime = data.row.startTime
        this.endTime = data.row.endTime
        this.temp.trainTime = Time
      }

      this.dataList(data)
      this.getNowTime()
    }

  },
  mounted() {
    console.log(this.sumUpData, 'sumUpData')
    this.temp.sumTime = this.getLocalTime()
  },
  methods: {
    dataList(data) {
      if (data.row.phaseNum) {
        listNode({ trainId: data.row.trainSumId }).then((res) => {
          res.data.forEach((item) => {
            if (item.phase == 'adminAudit') {
              this.temp.handleUserList = [{ label: item.handleUserName }]
              this.temp.opinion1 = item.opinion
              this.$forceUpdate()
            }
          })
        })
      }
    },
    openReview(val, type, title) {
      // 0为角色 1为角色（总经理，总监） 3为架构 4为架构多选 5部门多选
      if (val == 3) {
        this.SearchPersonnelData = {
          dialogVisible: true
        }
        this.personnelType = type
      }
    },
    // 选择人员（单选）
    dataListData(data) {
      if (data) {
        if (this.personnelType == 1) {
          this.temp.handleUserList = data
          //   console.log(data)
          //   this.temp.TeacherNameList = data[0]
          // } else if (this.personnelType == 2) {
          //   this.temp.handleUserId = data[0]
        } else {
          this.temp.handleUserList2 = data
        }
      }
      this.$forceUpdate()
    },
    // 获取当前时间;
    getLocalTime() {
      this.temp.sumTime = new Date()
      console.log(this.temp.sumTime, 'this.temp.sumTime')
    },
    save() {
      if (this.temp.phaseNum == 1) {
        if (!this.temp.handleUserList2) {
          this.$message.warning('选择人事行政')
          return
        }
        const data = {
          trainSumId: this.temp.trainSumId,
          opinion: this.temp.opinion1,
          result: 1,
          nextHandleUserId: this.temp.handleUserList2[0].id
        }
        if (!data.opinion) {
          this.$message.warning('意见不能为空')
          return
        }
        adminAudit(data).then((res) => {
          if (res.code == 1) {
            this.$message.success('提交成功')
          }
        })
      } else if (this.temp.phaseNum == 2) {
        const data = {
          trainSumId: this.temp.trainSumId,
          opinion: this.temp.opinion2,
          result: 1
        }
        if (!data.opinion) {
          this.$message.warning('意见不能为空')
          return
        }
        opinion(data).then((res) => {
          if (res.code == 1) {
            this.$message.success('提交成功')
          }
        })
      } else {
        if (!this.temp.handleUserList) {
          this.$message.warning('请选择主管领导')
          return
        }
        const data = {
          handleUserId: this.temp.handleUserList[0].id,
          trainId: this.temp.id,
          content: this.temp.content,
          sumContent: this.temp.sumContent,
          comment: this.temp.comment,
          trainTime: this.startTime + '/' + this.endTime,
          sumTime: this.temp.sumTime,
          certificateName: this.temp.certificateName,
          certificateFlag: this.temp.certificateFlag
        }
        if (!data.sumContent) {
          this.$message.warning('总结内容不能为空')
          return
        }
        if (data.certificateFlag == true) {
          if (!data.certificateName) {
            this.$message.warning('证书编号不可未空')
          }
        }

        save(data).then((res) => {
          if (res.code == 1) {
            this.$message.success('提交成功')
          }
        })
      }
      this.$emit('getList')
      this.sumUpData.dialogVisible = false
    },
    // 获取当前时间
    getNowTime() {
      var date = new Date()
      var year = date.getFullYear()
      var month =
        date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1
      var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      var minute =
        date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      var second =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      this.temp.sumTime =
        year +
        '-' +
        month +
        '-' +
        day +
        ' ' +
        hour +
        ':' +
        minute +
        ':' +
        second
    }
  }

}
</script>

<style>
</style>
